package com.aijk.ehm.sys.service;

import com.aijk.ehm.sys.mapper.SysRoleMapper;
import com.aijk.ehm.sys.model.SysRoleExample;
import com.aijk.ehm.sys.model.SysRole;
import com.aijk.ehm.sys.model.qo.SysRoleQo;
import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.utils.Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>  </p>
 * User: chendw
 * Date: 2016/11/15 17:14
 */
@Service
public class SysRoleService {
    @Autowired
    private SysRoleMapper roleMapper;

    public SysRoleExample getPageExample(SysRoleQo qo) {
        SysRoleExample example = new SysRoleExample();
        SysRoleExample.Criteria criteria = example.createCriteria();

        if (qo != null ) {
            if (Utils.isNotEmpty(qo.getSortway())) example.setOrderByClause(qo.getSortway());
            if (Utils.isNotEmpty(qo.getRoleName()))criteria.andRoleNameEqualTo( qo.getRoleName());
            if (Utils.isNotEmpty(qo.getSysFlag()))criteria.andSysFlagEqualTo( qo.getSysFlag());
            if (Utils.isNotEmpty(qo.getCompanyId()))criteria.andCompanyIdEqualTo( qo.getCompanyId());
            if (Utils.isNotEmpty(qo.getRoleIdNotEqual()))criteria.andRoleIdNotEqualTo( qo.getRoleIdNotEqual());
            if (Utils.isNotEmpty(qo.isCompanyIsNull())&&qo.isCompanyIsNull())criteria.andCompanyIdIsNull();
            if (Utils.isNotEmpty(qo.getInRolesId()))criteria.andRoleIdIn(qo.getInRolesId());

        }
        return example;
    }



    public PageInfo<SysRole> getPageInfo(PagingRequest pagingRequest, SysRoleQo qo) {
        SysRoleExample example = getPageExample(qo);
        PageHelper.startPage(pagingRequest.getPageNum(), pagingRequest.getPageSize());
        return new PageInfo(roleMapper.selectByExample(example));
    }

    public SysRole get(int roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }



    public SysRole update(SysRole role) {
        roleMapper.updateByPrimaryKeySelective(role);

        return roleMapper.selectByPrimaryKey(role.getRoleId());
    }

    public SysRole insert(SysRole role) {
        roleMapper.insert(role);
        return role;
    }

    public int delete(int roleId) {
        return roleMapper.deleteByPrimaryKey(roleId);
    }

    public List<SysRole> findAll(SysRoleQo qo) {
        return roleMapper.selectByExample(getPageExample(qo));
    }
}
